# Read the Solution [Here](https://quastor.org/cracking-the-coding-interview/moderate/number-swapper)

# Number Swapper

## Cracking the Coding Interview (CTCI) 16.1

<br />

## Question

Write a function to swap a number in place. You cannot use temporary variables.

```python
# Swap the values for a and b without using temporary variables!
a = 4
b = 6
# code to swap a and b
print(a) # 6
print(b) # 4
```

<details>
  <summary>Solution</summary>

  If we were allowed to use temporary variables, this question would be pretty easy...

  ```python
  temp = a 
  a = b
  b = temp
  ```

  But we can't use a temporary variable.

  <br />

  Instead, we can solve this question with some clever algebra!

  <br />

  To make this easier to understand, we'll give you the solution in Python first! Then, we'll explain how it works.

  ```python
  # Swap the values for a and b without using temporary variables!
  a = 4
  b = 6
  a = a - b
  b = a + b
  a = b - a
  print(a) # 6
  print(b) # 4
  ```

  <br />

  We'll use $a_0$ to represent the original value of $a$ and $b_0$ to represent the original value of $b$.

  < br />

  First, we set $a = a_0 - b_0$. That's line 4. 

  <br />

  Then, we set $b = a + b_0$. That's line 5.

  <br />

  In other words, this means we're setting $b = (a_0 - b_0) + b_0$. You can do the algebra to see that means we're setting $b = a_0$

  <br />

  Finally, we set $a = b - a$. That's line 6.

  <br />

  Now remember that in our previous two equations, we set $a = a_0 - b_0$ and $b = (a_0 - b_0) + b_0$.

  
  Therefore, we can substitute $b$ and $a$ in $b - a$ to get $a = ((a_0 - b_0) + b_0) - (a_0 - b_0)$.

  <br />

  You can simplify the algebra in that expression to get $a = a_0 - (a_0 - b_0)$. 
  
  Simplify that further to get $a = b_0$

  <br />

  The time complexity and space complexity are both $$O(1)$$.

  <br />

  
  Now, I know what you're wondering. How the hell am I supposed to figure this out in an interview?

  <br />

  Frankly, I have no idea. I don't write the interview questions, I'm just writing the solutions. If you're an interviewer and you're thinking of asking this question, please don't!
</details>
